#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import csv
import re
import os
import sys

def main_run(obj):
    filename = obj['filename']
    print(filename)
    fp = open(filename, "r")
    reader = csv.reader(fp)
    data = list(reader)
    fp.close()

    #科学计数法转换为浮点数表示法
    for i, y in enumerate(data):
        for j, x in enumerate(y):
            if re.search(r'^[+-]*\d+\.?\d*[Ee]*[+-]*\d+$', x) != None:
                data[i][j] = str(float(x))

    p, f = os.path.split(filename)
    f, t = os.path.splitext(f)
    f = f + '_float.csv'
    now_dir = os.getcwd()
    if p != "":
        os.chdir(p)

    fp = open(f, "w")
    writer = csv.writer(fp)
    writer.writerows(data)
    fp.close()

    os.chdir(now_dir)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("请输入正确参数：./csv_billing.py   ./PATH/XXX.csv")
        exit(-1)
    filename = sys.argv[1]
    if os.path.exists(filename) == False:
        print("请输入正确参数：./csv_billing.py   ./PATH/XXX.csv")
        exit(-1)

    main_run({'filename':filename})
